package com.poetize.plus.controller.ui;

import com.poetize.plus.entity.dto.CommentDTO;
import com.poetize.plus.entity.dto.page.CommentChildPageDTO;
import com.poetize.plus.entity.dto.page.CommentFloorPageDTO;
import com.poetize.plus.entity.vo.CommentFloorVO;
import com.poetize.plus.service.CommentDislikeService;
import com.poetize.plus.service.CommentLikeService;
import com.poetize.plus.service.CommentService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zuosy
 * @created 2024/4/11 13:11
 */
@Tag(name = "文章评论")
@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentUiController {

    private final CommentService commentService;

    private final CommentLikeService commentLikeService;

    private final CommentDislikeService commentDislikeService;

    @PermitAll
    @GetMapping("/getMaxFloor/{articleId}")
    @Operation(summary = "获取最大楼层")
    public R<Integer> getMaxFloor(@PathVariable("articleId") Long articleId){
        return R.ok(commentService.getMaxFloor(articleId));
    }

    @PermitAll
    @GetMapping("/getCommentByArticleId")
    @Operation(summary = "获取文章评论")
    public R<List<CommentFloorVO>> getCommentByArticleId(@Validated CommentFloorPageDTO commentFloorPageDTO){
        return R.ok(commentService.getCommentByArticleId(commentFloorPageDTO));
    }

    @PermitAll
    @GetMapping("/getChildComment")
    @Operation(summary = "获取楼层子评论")
    public R<List<CommentFloorVO.CommentFloorChild>> getChildComment(@Validated CommentChildPageDTO commentChildPageDTO){
        return R.ok(commentService.getChildComment(commentChildPageDTO));
    }

    @PostMapping("/publish")
    @Operation(summary = "发表评论")
    @PreAuthorize(value = "hasAnyRole('common','administrator')")
    public R<Boolean> publish(@RequestBody @Validated CommentDTO commentDTO){
        return R.ok(commentService.publish(commentDTO));
    }

    @GetMapping("/like/{commentId}")
    @Operation(summary = "评论点赞")
    @PreAuthorize(value = "hasAnyRole('common','administrator')")
    public R<Boolean> like(@PathVariable("commentId") Long commentId){
        return R.ok(commentLikeService.like(commentId));
    }

    @GetMapping("/dislike/{commentId}")
    @Operation(summary = "评论点踩")
    @PreAuthorize(value = "hasAnyRole('common','administrator')")
    public R<Boolean> dislike(@PathVariable("commentId") Long commentId){
        return R.ok(commentDislikeService.dislike(commentId));
    }
}